package com.price.common.viewbinding.base

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.price.common.extend.addNavigationBarHeightToMarginBottom
import com.price.common.extend.appLightMode
import com.price.common.extend.attachLanguage
import com.price.common.extend.contentView
import com.price.common.extend.doCustomCrashListener
import com.price.common.extend.immerseStatusBar
import com.price.common.viewbinding.ActivityBinding
import com.price.common.viewbinding.ActivityBindingDelegate

/**
 */
abstract class BaseBindingActivity<VB : ViewBinding> : AppCompatActivity(),
    ActivityBinding<VB> by ActivityBindingDelegate() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentViewWithBinding()
        runCatching {
            immerseStatusBar(appLightMode)

            if(navMarginBottom()){
                contentView.addNavigationBarHeightToMarginBottom()
            }
            onCreated(savedInstanceState)
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    abstract fun onCreated(savedInstanceState: Bundle?)

    //是否在底部抬高虚拟按键高度
    open fun navMarginBottom():Boolean{
        return true
    }
    override fun onResume() {
        super.onResume()
        runCatching {
            resume()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    override fun onPause() {
        super.onPause()
        runCatching {
            pause()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }
    open fun resume(){}
    open fun pause(){}

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase?.attachLanguage())
    }
}